#coding:utf8
"""
处理多任务，主要分为以下情况：
1、相同处理的多任务
2、不同处理的多任务
由于条件有限，基本都是采用的协程，未采用多进程方式
"""
import gevent
from gevent import monkey
monkey.patch_all()

import os
import pandas as pd
from utils.get_features import get_one_stock


def test():
    print("hello")


def multi_xc(func, args):
    g_list = []
    for i in range(5):
        g = gevent.spawn(func, args)
        g_list.append(g)

    gevent.joinall(g_list)
    for i, g in enumerate(g_list):
        print(i)
        print(g.value)


def run_multi_task_by_thread(history_dir, i, latest5date):
    dfs = []
    gevent_list = []
    for f in os.listdir(history_dir):
        g = gevent.spawn(get_one_stock, f, i, latest5date, history_dir)
        gevent_list.append(g)
    gevent.joinall(gevent_list)

    for i, g in enumerate(gevent_list):
        if isinstance(g.value, pd.DataFrame) and not g.value.empty:
            # print(f" dfs append!")
            dfs.append(g.value)

    return dfs